﻿using BPMN.Application.Contracts.DTO.Disposition;
using BPMN.Application.IService.Disposition;
using BPMN.Domain.Entity.Disposition;
using BPMN.Domain.Entity.Material;

using BPMN.Domain.Entity.Unit;
using BPMN.Domain.Interface;
using BPMN.Domain.ProcessModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.Application.Service
{
    /// <summary>
    /// 绩效工序
    /// </summary>
    public class DispositionService : IDispositionService
    {
        public DispositionService(IRepositoryBasetow<disposition> dis,IRepositoryBasetow<ProcessShows> ProcessShows, IRepositoryBasetow<Units> unit,IRepositoryBasetow<Products> Products)
        {
            Dis = dis;        //绩效
            this.ProcessShows = ProcessShows; //工序
            Unit = unit;      //单位
            this.Products = Products;  //工艺

        }

        public IRepositoryBasetow<disposition> Dis { get; }
        public IRepositoryBasetow<ProcessShows> ProcessShows { get; }
        public IRepositoryBasetow<Units> Unit { get; }
        public IRepositoryBasetow<Products> Products { get; }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dis"></param>
        /// <returns></returns>
        public bool AddDisposition(disposition dis)
        {
            return Dis.Add(dis);
        }
        /// <summary>
        /// 根据id获取绩效配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public disposition disposition(int id)
        {
            return Dis.GetAll().FirstOrDefault(disposition => disposition.Disposition_Id == id);
        }
        /// <summary>
        /// 分页显示联查
        /// </summary>
        /// <param name="name">工序名称</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="totalCount">总计</param>
        /// <param name="pagerCount">计数</param>
        /// <returns></returns>
        public List<DispositionDto> GetDispositions(string ?name,int pageIndex,int pageSize,out int totalCount,out int pagerCount)
        {
           var list=from dis in Dis.GetAll()
                    join pro in ProcessShows.GetAll() on dis.Process_Id equals pro.Process_Id
                    join unit in Unit.GetAll() on dis.unit_Id equals unit.Id
                    join pro1 in Products.GetAll() on dis.Pid equals pro1.PId
                    where dis.IsDel==false
                    select new DispositionDto
                    {
                        Performance_based_pay_Id = dis.Disposition_Id,
                        Process_Id = pro.Process_Id,
                        Process_Name = pro.Process_Name,
                        Unti_Id = unit.Id,
                        Unit_Name = unit.Name,
                        Pid = pro1.PId,
                        Pid_Name = pro1.PName,
                        Performance_based_pay_Money = dis.Price,
                        hour = dis.hours,
                        minute = dis.minutes,
                        second = dis.seconds,
                        isDel = dis.IsDel,
                  
                    };
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.Process_Name.Contains(name));
            }
            totalCount = list.Count();
            pagerCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        public List<ProcessShows> GetProcessShows()
        {
          return ProcessShows.GetAll().ToList();
        }

        public List<Products> GetProducts()
        {
            return Products.GetAll().ToList();
        }

        public List<Units> GetUnits()
        {
            return Unit.GetAll().ToList();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="longs"></param>
        /// <returns></returns>
        public bool plDelte(List<long> longs)
        {
            var list = Dis.GetAll();
            foreach (var item in list.ToList())
            {
                foreach (var id in longs)
                {
                    if(item.Disposition_Id==id)
                    {
                        item.IsDel = true;
                        Dis.Update(item);
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 修改绩效配置
        /// </summary>
        /// <param name="dis"></param>
        /// <returns></returns>
        public bool updateDisposition(disposition dis)
        {
          return Dis.Update(dis);
        }
    }
}
